package theOrbalEra.common.item;

import java.util.List;

import theOrbalEra.client.gui.GuiTheOrbalEra;
import theOrbalEra.client.handler.OrbalArtsHandlerClient;
import theOrbalEra.common.TheOrbalEra;
import theOrbalEra.common.util.DefultValue;
import theOrbalEra.common.util.GetName;
import theOrbalEra.common.util.OrbalUtils;

import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;


public class OrbalItemOrbment extends OrbalItem {

	private final OrbalArtsHandlerClient doOrbalArts=new OrbalArtsHandlerClient();

	public OrbalItemOrbment(int par1) {
		super(par1);
		this.setMaxStackSize(1);
		this.setUnlocalizedName("orbment");
	}
	
	public static void setEle(ItemStack par1ItemStack,int[] ele)
    {
        NBTTagCompound var1 = par1ItemStack.getTagCompound();

        if (var1 == null)
        {
            var1 = new NBTTagCompound();
            par1ItemStack.setTagCompound(var1);
        }

        NBTTagCompound var2 = var1.getCompoundTag("Orbment");

        if (!var1.hasKey("Orbment"))
        {
            var1.setCompoundTag("Orbment", var2);
        }
        var2.setIntArray("ele", ele);
        
    }
	public static int[] getEle(ItemStack par1ItemStack)
    {
		int[] ele={-1,-1,-1,-1,-1,-1};
		 NBTTagCompound var1 = par1ItemStack.getTagCompound();

	        if (var1 == null)
	        {
	            return ele;
	        }
	        else
	        {
	            NBTTagCompound var2 = var1.getCompoundTag("Orbment");
	            if(var2==null)
	            	return ele;
	            else {
	            	 
	            	 if(var2.hasKey("ele"))
	            		 return var2.getIntArray("ele");
	            	 else
	            		 return ele;
	            		 
	      
	           
	            }
	        }
        
    }
	
	@Override
	public boolean onItemUse(ItemStack par1ItemStack,
			EntityPlayer par2EntityPlayer, World par3World, int par4, int par5,
			int par6, int par7, float par8, float par9, float par10) {
		
		return super.onItemUse(par1ItemStack, par2EntityPlayer, par3World, par4, par5,
				par6, par7, par8, par9, par10);
	}

	@Override
	public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World,
			EntityPlayer par3EntityPlayer) {
		if(!par2World.isRemote){
		GuiTheOrbalEra gui =(GuiTheOrbalEra) FMLClientHandler.instance().getClient().ingameGUI;
		EntityPlayer player= par3EntityPlayer;
		if(player.inventory.mainInventory[player.inventory.currentItem]!=null&&player.inventory.mainInventory[player.inventory.currentItem].itemID==TheOrbalEra.orbment.itemID){
			ItemStack orb=player.inventory.mainInventory[player.inventory.currentItem];
			int[]able=OrbalItemOrbment.getAble(orb);
			int[] value1=OrbalUtils.calculateOrbal1(able);
			int[] value2=OrbalUtils.calculateOrbal2(able);
			int[] magic =OrbalUtils.getOrbalArts(value1,value2);
		
			if(magic[gui.currentSelect]!=-1){
			//	PacketOrbalArtsHandlerServer.doOrbalArts(magic[gui.currentSelect]);
				doOrbalArts.doOrbalArts(magic[gui.currentSelect], player, par2World);
			}
		}
		}
		return par1ItemStack;
	}

	public static void setAble(ItemStack par1ItemStack,int[] able)
    {
        NBTTagCompound var1 = par1ItemStack.getTagCompound();

        if (var1 == null)
        {
            var1 = new NBTTagCompound();
            par1ItemStack.setTagCompound(var1);
        }

        NBTTagCompound var2 = var1.getCompoundTag("Orbment");

        if (!var1.hasKey("Orbment"))
        {
            var1.setCompoundTag("Orbment", var2);
        }
        
        var2.setIntArray("quartz", able);
        
    }
	

	public static int[] getAble(ItemStack par1ItemStack)
    {
		int[] able={-1,-1,-1,-1,-1,-1};
		 NBTTagCompound var1 = par1ItemStack.getTagCompound();

	        if (var1 == null)
	        {
	            return able;
	        }
	        else
	        {
	        	 NBTTagCompound var2 = var1.getCompoundTag("Orbment");
		            if(var2==null)
		            	return able;
		            else if(var2.hasKey("quartz"))
		            		 return var2.getIntArray("quartz");
		            	 else
		            		 return able;
		            	 
	           
	        }
        
    }
	
	public static void setCao(ItemStack par1ItemStack,int[] open)
    {
		int[] cao={1,0,0,0,0,0};
        NBTTagCompound var1 = par1ItemStack.getTagCompound();

        if (var1 == null)
        {
            var1 = new NBTTagCompound();
            par1ItemStack.setTagCompound(var1);
        }

        NBTTagCompound var2 = var1.getCompoundTag("Orbment");

        if (!var1.hasKey("Orbment"))
        {
            var1.setCompoundTag("Orbment", var2);
        }
        if(open!=cao)
        var2.setIntArray("cao", open);
        else
        	var2.removeTag("cao");
        
    }
	public static int[] getCao(ItemStack par1ItemStack)
    {
		int[] open={1,0,0,0,0,0};
		 NBTTagCompound var1 = par1ItemStack.getTagCompound();

	        if (var1 == null)
	        {
	            return open;
	        }
	        else
	        {
	        	 NBTTagCompound var2 = var1.getCompoundTag("Orbment");
		            if(var2==null)
		            	return open;
		            else {
		            	 if(var2.hasKey("cao"))
		            		 return var2.getIntArray("cao");
		            	 else
		            		 return open;
		            	
		            		 
		            	
		            }
	        }
        
    }
	
	 @SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	@SideOnly(Side.CLIENT)
	public void addInformation(ItemStack par1ItemStack,EntityPlayer par2EntityPlayer, List par3List, boolean par4) {
	 
	 int[] able=OrbalItemOrbment.getAble(par1ItemStack);
	 for(int i:able)
		 if(i!=-1){
			 par3List.add(GetName.localize("quartz.".concat(GetName.getQuartzName(i))));
			 }
		 else{
			 par3List.add(GetName.localize("display.none"));
		 }
	
			 
	
	
	 
	 
	 }
	 
	 @Override
		@SideOnly(Side.CLIENT)
		public void registerIcons(IconRegister par1IconRegister) {
			this.itemIcon=par1IconRegister.registerIcon(DefultValue.ModName+":orbment");
			}

	

}
